cap log close 
log using "${logpath}firmlevel_analysis.log", replace

/*******************************************************************************
firmlevel_analysis.do

This code merges firm-level USRDS data with MUP data.
*******************************************************************************/

qui {
	use using "${cleandatapath}firmlevel.dta", clear

	replace year=year(dofm(t))

	bysort firmstate: egen npi=mode(NPI)
	tostring npi, replace

	collapse (sum) nonEdial_pmt, by(npi year)

	merge m:1 npi year using "${cleandatapath}MUP.dta"
	keep if _merge==3
	drop _merge

	destring npi, gen(firm_num)

	sort firm_num year
	tsset firm_num year
	tsfill, full
	foreach var in avg_mdcr_pymt_amt tot_srvcs {
		forvalues e = 425/429 {
			replace `var'A0`e'=0 if missing(`var'A0`e')
		}
	}

	gen tot_rev=0
	forvalues e = 425/429 {
		replace tot_rev=tot_rev + avg_mdcr_pymt_amtA0`e' * tot_srvcsA0`e'
	}
	gen log_tot_rev=log(tot_rev)
	
	replace nonEdial_pmt=0 if missing(nonEdial_pmt)
	gen log_nonEdial_pmt=log(nonEdial_pmt)

	gen oth_pmt=tot_rev-nonEdial_pmt

	bysort firm_num: egen state=mode(prvdr_state_abrvtn)

	gen wave1=state=="NJ" | state=="PA" | state=="SC"
	gen wave2=state=="DC" | state=="DE" | state=="MD" | state=="NC" | state=="VA" | state=="WV"

	gen group1=1 if wave1==1 & (year==2014 | year==2015)
	replace group1=1 if wave1==0 & wave2==0 & (year==2014 | year==2015)

	gen exit_market=nonEdial_pmt==0
	gen complete_exit=tot_rev==0

	su oth_pmt if wave1==1 & year==2014 & nonEdial_pmt>0, d
	gen oth_size=1 if oth_pmt<=`r(p25)' & nonEdial_pmt>0 & group1==1 & year==2014
	replace oth_size=2 if oth_pmt>`r(p25)' & !missing(oth_pmt) & nonEdial_pmt>0 & group1==1 & year==2014
	replace oth_size=3 if oth_pmt>`r(p50)' & !missing(oth_pmt) & nonEdial_pmt>0 & group1==1 & year==2014
	replace oth_size=4 if oth_pmt>`r(p75)' & !missing(oth_pmt) & nonEdial_pmt>0 & group1==1 & year==2014
	bysort firm_num: egen size_oth=max(oth_size)

	gen post_treat=(wave1==1 & year==2015)
	gen size2_inter=(wave1==1 & year==2015 & size_oth==2)
	gen size3_inter=(wave1==1 & year==2015 & size_oth==3)
	gen size4_inter=(wave1==1 & year==2015 & size_oth==4)

	encode state, gen(state_code)

	estimates clear
	eststo: reghdfe log_tot_rev post_treat size2_inter size3_inter size4_inter if group1==1, a(i.year##i.size_oth i.state_code##i.size_oth) vce(cluster state)
		estadd ysumm
		eststo est1,addscalars(Year_FE 1)
		eststo est1,addscalars(Dist_FE 1)
	eststo: reghdfe log_nonEdial_pmt post_treat size2_inter size3_inter size4_inter if group1==1, a(i.year##i.size_oth i.state_code##i.size_oth) vce(cluster state)
		estadd ysumm
		eststo est2,addscalars(Year_FE 1)
		eststo est2,addscalars(Dist_FE 1)
	eststo: reghdfe exit_market post_treat size2_inter size3_inter size4_inter if group1==1, a(i.year##i.size_oth i.state_code##i.size_oth) vce(cluster state)
		estadd ysumm
		eststo est3,addscalars(Year_FE 1)
		eststo est3,addscalars(Dist_FE 1)
	eststo: reghdfe complete_exit post_treat size2_inter size3_inter size4_inter if group1==1, a(i.year##i.size_oth i.state_code##i.size_oth) vce(cluster state)
		estadd ysumm
		eststo est4,addscalars(Year_FE 1)
		eststo est4,addscalars(Dist_FE 1)
	label var log_tot_rev "\shortstack{Total\\ Revenue (Log)}"
	label var log_nonEdial_pmt "\shortstack{Non-Emergent Dialysis\\ Revenue (Log)}"
	label var exit_market "\shortstack{Exits Non-Emergent\\ Dialysis Market}"
	label var complete_exit "\shortstack{Completely\\ Exits}"
	label var post_treat "Prior Authorization"
	label var size2_inter "Prior Auth. $\times$ Size Quartile 2"
	label var size3_inter "Prior Auth. $\times$ Size Quartile 3"
	label var size4_inter "Prior Auth. $\times$ Size Quartile 4"
	esttab using ${outpath}Table_A33.tex, replace ///
		frag se lab star(+ 0.1 * 0.05 ** 0.01 *** 0.001) ///
		drop(_cons) stats(Year_FE Dist_FE ymean N, ///
			label("Quartile-by-State FE" "Quartile-by-Year FE" "Dep. Var. Mean" "Observations"))
}
		
di exp(-1.140)-1 // Referenced in Appendix I, Paragraph 4
di exp(-2.358)-1 // Referenced in Appendix I, Paragraph 4
		
log close